﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for TinTucProvider
/// </summary>
namespace EHTWebsite.Code
{
    public class TinTucProvider
    {
        public static DataSet GetTinTucs()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucs";
            command.CommandType = CommandType.StoredProcedure;
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }

        public static TinTuc GetTinTucByTinTucID(Int32 TinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucByTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucID", TinTucID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            TinTuc theTinTuc = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theTinTuc = new TinTuc();
                theTinTuc.TinTucID = Int32.Parse(ds.Tables[0].Rows[0]["TinTucID"].ToString());
                theTinTuc.TieuDeVN = ds.Tables[0].Rows[0]["TieuDeVN"].ToString();
                theTinTuc.TieuDeOther = ds.Tables[0].Rows[0]["TieuDeOther"].ToString();
                theTinTuc.NoiDungVN = ds.Tables[0].Rows[0]["NoiDungVN"].ToString();
                theTinTuc.NoiDungOther = ds.Tables[0].Rows[0]["NoiDungOther"].ToString();
                theTinTuc.NgayTao = DateTime.Parse(ds.Tables[0].Rows[0]["NgayTao"].ToString());
                theTinTuc.NguoiTao = ds.Tables[0].Rows[0]["NguoiTao"].ToString();
                theTinTuc.TomTatVN = ds.Tables[0].Rows[0]["TomTatVN"].ToString();
                theTinTuc.TomTatOther = ds.Tables[0].Rows[0]["TomTatOther"].ToString();
                theTinTuc.HinhAnhUrl = ds.Tables[0].Rows[0]["HinhAnhUrl"].ToString();
                theTinTuc.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theTinTuc.DanhMucTinTucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucTinTucID"].ToString());
                theTinTuc.NoiBat = Boolean.Parse(ds.Tables[0].Rows[0]["NoiBat"].ToString());
                theTinTuc.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theTinTuc.Icon = ds.Tables[0].Rows[0]["Icon"].ToString();
                theTinTuc.Trangthai = Boolean.Parse(ds.Tables[0].Rows[0]["TrangThai"].ToString());
            }

            return theTinTuc;
        }

        public static DataSet GetTinTucsByFillterForBackEnd(String tieuDeVn, String tieuDeOther, Int32 danhMucTinTucID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucsByFillterForBackEnd";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TieuDeVN", tieuDeVn));
            command.Parameters.Add(new SqlParameter("@TieuDeOther", tieuDeOther));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", danhMucTinTucID)); 
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);

            return ds;
        }

        public static DataSet GetTinTucsByFillterForFrontEnd(String tieuDeVn, String tieuDeOther, Int32 danhMucTinTucID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucsByFillterForFrontEnd";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TieuDeVN", tieuDeVn));
            command.Parameters.Add(new SqlParameter("@TieuDeOther", tieuDeOther));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", danhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);

            return ds;
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 16/02/2012
        /// Description : 
        /// </summary>
        /// <param name="theTinTuc"></param>
        /// <param name="isHot"></param>
        /// <param name="isAdd"></param>
        /// <returns></returns>
        public static Int32 IUTinTucs(TinTuc theTinTuc, Boolean isHot, Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUTinTuc";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theTinTucIDParameter = new SqlParameter();

            theTinTucIDParameter.ParameterName = "@TinTucIDOut";
            theTinTucIDParameter.Direction = ParameterDirection.Output;
            theTinTucIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theTinTucIDParameter);
            command.Parameters.Add(new SqlParameter("@TinTucID", theTinTuc.TinTucID));
            command.Parameters.Add(new SqlParameter("@TieuDeVN", theTinTuc.TieuDeVN));
            command.Parameters.Add(new SqlParameter("@TieuDeOther", theTinTuc.TieuDeOther));
            command.Parameters.Add(new SqlParameter("@NoiDungVN", theTinTuc.NoiDungVN));
            command.Parameters.Add(new SqlParameter("@NoiDungOther", theTinTuc.NoiDungOther));
            command.Parameters.Add(new SqlParameter("@NguoiTao", theTinTuc.NguoiTao));
            command.Parameters.Add(new SqlParameter("@TomTatVN", theTinTuc.TomTatVN));
            command.Parameters.Add(new SqlParameter("@TomTatOther", theTinTuc.TomTatOther));
            command.Parameters.Add(new SqlParameter("@HinhAnhUrl", theTinTuc.HinhAnhUrl));
            command.Parameters.Add(new SqlParameter("@SapXep", theTinTuc.SapXep));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", theTinTuc.DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@NoiBat", theTinTuc.NoiBat));
            command.Parameters.Add(new SqlParameter("@Icon", theTinTuc.Icon));
            command.Parameters.Add(new SqlParameter("@Hot", isHot));
            command.Parameters.Add(new SqlParameter("@Trangthai", theTinTuc.Trangthai));
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));
            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@TinTucIDOut"].Value.ToString());
        }


        /// <summary>
        /// Update danh muc tin tuc boi numberorder
        /// author: chiennv
        /// create: 08/02/2012
        /// </summary>
        /// <param name="DanhMucTinTucID"></param>
        /// <returns></returns>
        public static void UpdateNumberOfOrderByTinTucID(Int32 TinTucID, Int32 SapXep)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderByTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucID", TinTucID));
            command.Parameters.Add(new SqlParameter("@SapXep", SapXep));
            DataProvider.ExecuteNoneQuery(command);
        }


    

        public static void DeleteTinTucByTinTucID(Int32 TinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteTinTucByTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucID", TinTucID));
            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Lay tin tuc theo DanhMucTinTucID
        /// author: chiennv
        /// create: 6/2/2012
        /// </summary>
        /// <param name="TinTucID"></param>
        /// <param name="DanhMucTinTucID"></param>
        /// <param name="NoiBat"></param>
        public static DataSet GetTintucbyDanhMucTinTucID(Int32 DanhMucTinTucID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);

            return ds;
        }

        public static void UpdateTinTucHot(Int32 TinTucID, Int32 DanhMucTinTucID, Boolean NoiBat)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UTinTucHot";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucHotID", TinTucID));
            command.Parameters.Add(new SqlParameter("@NoiBat", NoiBat));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));

            DataProvider.ExecuteNoneQuery(command);
        }



        public static void ChangePublishByTinTucID(int TinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucID", TinTucID));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}